package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import model.Stack;

public abstract class TabledFilteringPanel extends JPanel implements Updatable {
    protected JTable tbl;
    private DefaultTableModel model;
    protected Vector<String> columnNames = new Vector<String>();
    protected Vector<Vector<Object>> data = new Vector<Vector<Object>>();

    public TabledFilteringPanel() {

	// setSize(600, 500);
	// setLocation(10, 10);

	model = new DefaultTableModel();
	setLayout(new BorderLayout(0, 0));

	tbl = new JTable(model) {
	    @Override
	    public boolean isCellEditable(int rowIndex, int colIndex) {
		return false; // Disallow the editing of any cell
	    }
	};

	// tbl.setPreferredScrollableViewportSize(new Dimension(500, 70));
	// tbl.setFillsViewportHeight(true);

	tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

	JScrollPane scrollPane = new JScrollPane(tbl);

	add(scrollPane);

	JPanel panel = new JPanel();
	FlowLayout flowLayout = (FlowLayout) panel.getLayout();
	flowLayout.setAlignment(FlowLayout.RIGHT);
	add(panel, BorderLayout.SOUTH);

	JButton btn = new JButton("Update");
	panel.add(btn);
	btn.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		int index = tbl.getSelectedRow();
		Stack stack = (Stack) tbl.getModel().getValueAt(index, 0);
		if (stack != null) {
		    StackInfoDialog info = new StackInfoDialog(stack,
			    TabledFilteringPanel.this);
		    info.setVisible(true);
		}
	    }
	});

	tbl.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent event) {
		if (event.getClickCount() == 2) {
		    int index = tbl.getSelectedRow();
		    Stack stack = (Stack) tbl.getModel().getValueAt(index, 0);
		    if (stack != null) {
			StackInfoDialog info = new StackInfoDialog(stack,
				TabledFilteringPanel.this);
			info.setVisible(true);
		    }
		}
	    }
	});
    }

    public void setData(Vector<String> columnNames, Vector<Vector<Object>> data) {
	this.columnNames = columnNames;
	this.data = data;
	DefaultTableModel newModel = new DefaultTableModel(this.data,
		this.columnNames);
	tbl.setModel(newModel);
    }

    @Override
    public void updateView() {
	int index = tbl.getSelectedRow();
	data.clear();
	generateDataVector();
	// setData(columnNames, data);
	((DefaultTableModel) tbl.getModel()).fireTableDataChanged();
	if (tbl.getRowCount() > index && index > -1) {
	    tbl.setRowSelectionInterval(index, index);
	}
    }

    protected abstract void generateDataVector();

    // Get methods for data and column names

}
